<template>
  <div>
    <div style="float: left">
      <el-button @click="goto">上报</el-button>
      <el-button v-show="this.y_or_n" @click="deletedata()">删除</el-button>
      <div style="color: red">
        温馨提示：双击数据左上角会弹出删除按钮，可进行删除操作
      </div>
    </div>

    <div style="float: right">
      <el-input v-model="obj.province" placeholder="请输入内容"></el-input>
    </div>
    <el-table :data="dataList.list" style="width: 100%" @row-dblclick="set_id">
      <el-table-column
        prop="id"
        label="编号"
        width="50"
        align="center"
        fixed="left"
      >
      </el-table-column>
      <el-table-column prop="country" label="国家" align="center">
      </el-table-column>
      <el-table-column prop="province" label="省份" align="center">
      </el-table-column>
      <el-table-column prop="city" label="市" align="center"> </el-table-column>
      <el-table-column prop="area" label="区" align="center"> </el-table-column>
      <el-table-column prop="confirmed" label="现有确诊" align="center">
      </el-table-column>
      <el-table-column prop="suspected" label="疑似" align="center">
      </el-table-column>
      <el-table-column prop="dead" label="死亡人数" align="center">
        <template v-slot="scope">
          <el-tag type="danger">{{ scope.row.dead }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="cure" label="治愈" align="center">
        <template v-slot="scope">
          <el-tag type="success">{{ scope.row.cure }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="severe" label="累计确诊" align="center">
      </el-table-column>
      <el-table-column prop="outside" label="境外输入" align="center">
      </el-table-column>
      <el-table-column
        prop="inputTime"
        label="录入时间"
        width="180"
        align="center"
        fixed="right"
      >
        <template v-slot="scope">
          {{ scope.row.inputTime | dateFMT }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="prev, pager, next"
      :total="dataList.total"
      :page-size="obj.pageSize"
      :current-page="obj.page"
      @current-change="change_page"
    >
    </el-pagination>
  </div>
</template>

<script>
// 从vuex中导入辅助函数
// 辅助函数：帮助我们快速获取状态机中的变量或者方法
import { mapActions, mapState } from "vuex";
export default {
  //计算属性
  computed: {
    // state和getters的属性传输要用computed
    // 从dataList.js文件的state中获取dataList变量
    ...mapState("dataList", ["dataList"]),
  },
  watch: {
    obj: {
      handler: function () {
        this.page_query(this.obj);
      },
      deep: true,
    },
  },
  data() {
    return {
      obj: {
        page: 1,
        pageSize: 5,
        province: "",
      },
      total: 0,
      data_id: {},
      y_or_n: false,
    };
  },
  created() {
    this.page_query(this.obj);
  },
  methods: {
    change_page(val) {
      this.obj.page = val;
    },
    // 获取行id
    set_id(val) {
      this.data_id = val;
      // 显示删除按钮
      this.y_or_n = true;
    },
    ...mapActions("dataList", ["delete_data"]),
    // 删除数据
    deletedata() {
      this.$confirm("是否删除第" + this.data_id.id + "条数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delete_data(this.data_id).then((res) => {
            if (res.status == 200) {
              this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success",
              });
              this.y_or_n = false;
            }
            this.page_query(this.obj);
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
          this.y_or_n = false;
        });
    },
    // 从dataList.js文件的actions中获取page_query方法
    ...mapActions("dataList", ["page_query"]),
    //跳转
    goto() {
      this.$router.push({
        path: "/data_List/updata",
      });
    },
  },
};
</script>